﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO.Compression;
using Telerik.Web.UI;
using Core.Security.Business;
using Core.Common.Session;
using Core.Message;
using Core.ExceptionHandling;
using Core.Security.CoreMembership;
using Core.NTier.UserInterface;
using System.Collections;
using Core.Controls.Message;
using Core.Message.Entities;
using Core.Configs;
using System.Web.UI.HtmlControls;

public partial class SiteMaster : BaseMasterPage
{
    #region Const
    private const string _TELERIK_SKIN = "Office2007";
    private const string _CSS_LINK = "~/App_Themes/{0}/Styles/{1}";
    #endregion

    #region Property
    /// <summary>
    /// Số dòng tối đa trên lưới
    /// </summary>
    public int GrigMaxPageSize {
        get { return CoreConfigManager.InterfacePolicy.MaxPageSize; }
    }
    #endregion

    #region Page Event
    /// <summary>
    /// Sự kiện load trang
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            this.lblFormTitle.Text = base.GetFunctionTitle();
            this.hdfSiteTitle.Value = this.lblFormTitle.Text;

            if (!this.IsPostBack)
            {
                
                this.hdfConfigTitle.Value = base.ConfigTtitle;
                this.SetFormForPopup();                          
            }
           
        }
        catch (Exception ex)
        {
            AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
            MessageBox.Show(message);
        }
    }
       
    /// <summary>
    /// Sự kiện khởi tạo trang
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Init(object sender, EventArgs e)
    {
        try
        {           
            //Set skin cho tất cả Telerik Control trong trang
            //Đưa tất cả control telerik về 1 style, 
            //thuận tiện cho quá trình style cho trang
            this.eplStyleManeger.Skin = _TELERIK_SKIN;
        }
        catch (Exception ex)
        {
            AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
            MessageBox.Show(mess);
        }
    }

    //Xử lý sự kiện PreRender cho maincontent
    protected void MainContent_OnPreRender(object sender, EventArgs e) {
        try
        {
            if (!this.IsPostBack)
            {

                
            }
            //Link phân hệ
            this.lblModule.Text = this.mnuTop.ModuleDefaultPath;
            //Link liên kết
            this.lblCurrentPage.Text = this.leftMenu.FunctionPath;

            string pageTitle = this.hdfSiteTitle.Value;
            if (string.IsNullOrEmpty(this.hdfConfigTitle.Value) == false)
                pageTitle = this.hdfConfigTitle.Value + " - " + pageTitle;
            this.Page.Title = pageTitle;
        }
        catch (Exception ex)
        {
            AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
            MessageBox.Show(message);
        }
    }
    protected override void Render(HtmlTextWriter writer)
    {
        doCompression();
        base.Render(writer);
    }
    protected override void OnPreRender(System.EventArgs e)
    {
        base.OnPreRender(e);

        SetupQsfResourcesLinks();
       
    }
    private void SetupQsfResourcesLinks()
    {
        QsfScriptsLink.Attributes["href"] = Page.ResolveUrl(QsfScriptsLink.Attributes["href"]);
        
        //QsfStyleSheetsLink.Attributes["href"] = Page.ResolveUrl(QsfStyleSheetsLink.Attributes["href"]);

        // Add a script tag pointing to the scripts location.
        HtmlGenericControl script = new HtmlGenericControl("script");
        script.Attributes["src"] = QsfScriptsLink.Attributes["href"];
        script.Attributes["type"] = "text/javascript";

        ScriptTagPlaceHolder.Controls.Add(script);
        QsfScriptsLink.Visible = false;

        //QsfScriptsLink1.Attributes["href"] = Page.ResolveUrl(QsfScriptsLink1.Attributes["href"]);
        //HtmlGenericControl script1 = new HtmlGenericControl("script");
        //script1.Attributes["src"] = QsfScriptsLink1.Attributes["href"];
        //script1.Attributes["type"] = "text/javascript";
        //ScriptTagPlaceHolder1.Controls.Add(script1);
        //QsfScriptsLink1.Visible = false;
        // Set the ID to an empty string so that id does not render.
        //QsfStyleSheetsLink.ID = String.Empty;

        // Hide the <link> tag as it cannot load the scripts.
        
        
    }
    public static void doCompression()
    {
        HttpContext context = HttpContext.Current;
        HttpRequest request = context.Request;
        string acceptEncoding = request.Headers["Accept-Encoding"];
        HttpResponse response = context.Response;
        if (!string.IsNullOrEmpty(acceptEncoding))
        {
            acceptEncoding = acceptEncoding.ToUpperInvariant();
            response.Filter = new GZipStream(context.Response.Filter,
                                  CompressionMode.Compress);
            if (acceptEncoding.Contains("GZIP"))
            {
                response.AppendHeader("Content-encoding",
                                      "gzip");
            }
            else if (acceptEncoding.Contains("DEFLATE"))
            {
                response.AppendHeader("Content-encoding",
                                      "deflate");
            }
        }
        response.Cache.VaryByHeaders["Accept-Encoding"] = true;
    }
    #endregion

    #region Private Method
    /// <summary>
    /// Nếu là popup thì ẩn toàn bộ : LeftMenu, TopHeader, Footer
    /// </summary>
    private void SetFormForPopup() {
        try
        {
            
            string IsPopup = this.Request.QueryString["IsPopUp"];
            if (string.IsNullOrEmpty(IsPopup)) IsPopup = "0";
            if (IsPopup == "1")
            {
                RadPane panelTop = (RadPane)this.splMain.FindControl("pnlTop");
                panelTop.Visible = false;
                RadPane panelBottom = (RadPane)this.splMain.FindControl("pnlBottom");
                panelBottom.Visible = false;

                RadPane panelMenu = (RadPane)this.splMain.FindControl("RadPanelMenu");
                panelMenu.Visible = false;

                RadSplitBar splLeft = (RadSplitBar)this.splMain.FindControl("splLeft");
                splLeft.Visible = false;

                RadSplitBar splTop = (RadSplitBar)this.splMain.FindControl("splTop");
                splTop.Visible = false;
            }
        }
        catch (Exception ex)
        {
            AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
            MessageBox.Show(message);
        }
    }

    #endregion

}
